#!/usr/bin/env ruby
# A renderer to output logger notifications as plain text.
class RedBook::TextRenderer < RedBook::Renderer

	RedBook::Renderer.protected_instance_methods(false).each do |m|
		define_method(m.to_sym) do
			super.uncolorize.encode_out
		end	
	end
	
	protected 
	
	# Prevents rendering of success elements.
	def success(element)
		nil
	end
	
	# Render command elements.
	def command(element)
		command = "# Operation: #{element.attributes[:operation].to_s.humanize}"
		element.attributes.each_pair do |i, v|
			case i.to_s
			when /avg|total|count|duration/ then
				command += "(#{i.to_s.humanize})"
			when 'negate' then;
			when 'operation' then;
			when 'message' then
				if element.attributes[:negate] then
					command += "\n   - Filter - Message doesn't match #{v}" unless v.blank?
				else
					command += "\n   - Filter - Message matches #{v.to_s}" unless v.blank?
				end
			when /pending|completed|plain/ then
				command += "\n   - Filter - Activity is #{i.to_s}" unless v.blank?
			else
				command +="\n   - #{i.to_s.humanize}: #{v.to_s}"
			end
		end
		rb_command = element.to_command
		unless rb_command[:tags].blank?
			alternatives = 1 
			rb_command[:tags].each do |ts|
				if ts.is_a? Hash then
					command += "\n   - Alternative ##{alternatives}"
					command += "\n     - Filter - Tagged as: #{ts[:positive].join(', ')}" unless ts[:positive].blank?
					command += "\n     - Filter - Not tagged as: #{ts[:negative].join(', ')}" unless ts[:negative].blank?
					alternatives += 1
				else
					command += "\n   - Tagged as: #{ts.join(', ')}" unless ts.blank?
				end
			end
		end
		command.encode_out
	end
	
end

